import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/pages/house_recommend/house_recommend_item.dart';
import 'package:flutter_app/utils/dio_http.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class HouseRecommendPage extends StatefulWidget {
  const HouseRecommendPage({Key? key}) : super(key: key);

  @override
  _HouseRecommendPageState createState() => _HouseRecommendPageState();
}

class _HouseRecommendPageState extends State<HouseRecommendPage> {
  List _recommendList = [];
  _getRecommend() async {
    var res = await DioHttp.of(context).get('/home/groups');
    var resString = json.decode(res.toString());
    var status = resString['status'];
    String desc = resString["description"] ?? "内部错误";
    if (status.toString().startsWith('2')) {
      var recommendList = resString["body"];
      setState(() {
        _recommendList = recommendList;
      });
      // print(_recommendList);
    } else {
      EasyLoading.showError(desc);
    }
  }

  void initState() {
    _getRecommend();

    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('房屋推荐'),
      ),
      body: Container(
        padding: const EdgeInsets.only(left: 20.0),
        child: Wrap(
            spacing: 20.0,
            children: _recommendList
                .map((e) => (HouseRecommendItem(
                      desc: e["desc"],
                      imgSrc: e["imgSrc"],
                      title: e["title"],
                    )))
                .toList()),
      ),
    );
  }
}
